package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class InquireByLetterViewModel extends AndroidViewModel {
    public MutableLiveData<String> saveSuccess = new MutableLiveData<>();
    public InquireByLetterViewModel(@NonNull @NotNull Application application) {
        super(application);
    }
    public MutableLiveData<String> uploadRefresh = new MutableLiveData<>();


    //上传多图片
    public void uploadMutiFileModel(Context context, File file, int completeNum) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        APIManager.getInstance().getUploadAPI().addFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    uploadRefresh.postValue((String) response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(getApplication(), t.getMessage());
            }
        });
    }
    public void saveUserInquiry(Context context, Map<String, Object> map) {
        DialogHelper.showProgressDialog(context, null, "提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().saveEnquiry(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_FAIL)) {
                    MToast.showToast(getApplication(), response.body().getMsg());
                } else {
                    MToast.showToast(getApplication(), "提交成功，可至“我的”- “函询”查看");
                    saveSuccess.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
